package com.qwt.part_time_api.common.provider;

import com.qwt.part_time_api.error.ErrorCode;
import com.qwt.part_time_api.utils.JsonAuthentication;
import com.qwt.part_time_api.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 未登录时访问无权访问资源处理逻辑
 *
 * @author 君子慎独
 */

@Component
@Slf4j
public class LocalAuthenticationEntryPoint extends JsonAuthentication implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        log.info("未登录时访问无权访问资源处理逻辑");
        Result result = Result.fail(ErrorCode.USER_NO_LOGIN.getErrCode(), ErrorCode.USER_NO_LOGIN.getErrMsg());
        this.writeJson(httpServletRequest, httpServletResponse, result);
    }
}


